﻿using System;
using System.Net.Http;
using Common.Logging;
using In.Api.Dto.HisWs;
using In.Api.Dto.LisWs;
using Newtonsoft.Json;
using Out.Service.HttpClients;

namespace Out.Service.Services
{
    public class BizReport 
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(BizReport));

        /// <summary>
        /// 获取检验单列表
        /// </summary>
        /// <param name="cardNo">就诊卡号</param>
        /// <param name="begindate">开始时间</param>
        /// <param name="enddate">结束时间</param>
        /// <returns></returns>
        public ResResult<GetExamInfoResponse> InspectionList(string cardNo,DateTime begindate,DateTime enddate)
        {
            var result = new ResResult<GetExamInfoResponse>();
            var request = new GetExamInfoRequest();
            request.CardNo = cardNo;
            request.ItemType = "C";
            request.BeginDate = begindate.ToString("yyyy-MM-dd");
            request.EndDate = enddate.ToString("yyyy-MM-dd");

            GetExamInfoResponse response;

            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetExamInfo", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<GetExamInfoResponse>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院内获取检验单列表出错,信息：{0}", JsonConvert.SerializeObject(ex));
                result.Result = Result.未返回;
                result.Message = "调用院内获取检验单列表出错!";
                return result;
            }
            result.Response = response;

            
            if (response.ResultCode == "0")
            {
                result.Result = Result.成功;
                return result;
            }
            else
            {
                result.Result = Result.失败;
                result.Message = response.ResultDesc;
                return result;
            }

        }

        /// <summary>
        /// 获取检验单列表
        /// </summary>
        /// <param name="appNo">app号</param>
        /// <returns></returns>
        public ResResult<GetInspectionDetailResponse> InspectionDetail(string appNo)
        {
            if (appNo == null) throw new ArgumentNullException(nameof(appNo));
            var result = new ResResult<GetInspectionDetailResponse>();

            var request = new GetInspectionDetailRequest();
            request.AppNo = appNo;

            GetInspectionDetailResponse response;

            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("LisWs/GetInspectionDetail", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<GetInspectionDetailResponse>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院检验单明细Api出错,信息：{0}", JsonConvert.SerializeObject(ex));
                result.Result = Result.未返回;
                result.Message = "调用院检验单明细Api出错!";
                return result;
            }
            result.Response = response;

            if (response.ResultCode == "0")
            {
                result.Result = Result.成功;
                return result;
            }
            else
            {
                result.Result = Result.失败;
                result.Message = response.ResultDesc;
                return result;
            }
        }

        /// <summary>
        /// 获取检查单列表
        /// </summary>
        /// <param name="cardNo">就诊卡号</param>
        /// <param name="begindate">开始时间</param>
        /// <param name="enddate">结束时间</param>
        /// <returns></returns>
        public ResResult<GetExamInfoResponse> CheckList(string cardNo, DateTime begindate, DateTime enddate)
        {
            var result = new ResResult<GetExamInfoResponse>();
            var request = new GetExamInfoRequest();
            request.CardNo = cardNo;
            request.ItemType = "D";
            request.BeginDate = begindate.ToString("yyyy-MM-dd");
            request.EndDate = enddate.ToString("yyyy-MM-dd");

            GetExamInfoResponse response;

            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetExamInfo", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<GetExamInfoResponse>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院内获取检查单列表出错,信息：{0}", JsonConvert.SerializeObject(ex));
                result.Result = Result.未返回;
                result.Message = "调用院内获取检查单列表出错!";
                return result;
            }
            result.Response = response;

            if (response.ResultCode == "0")
            {
                result.Result = Result.成功;
                return result;
            }
            else
            {
                result.Result = Result.失败;
                result.Message = response.ResultDesc;
                return result;
            }
        }

        /// <summary>
        /// 获取检查单明细
        /// </summary>
        /// <param name="cardNo">就诊卡号</param>
        /// <param name="examId">检查单Id</param>
        /// <returns></returns>
        public ResResult<GetExamDetailResponse> CheckDetail(string cardNo, string examId )
        {
            var result = new ResResult<GetExamDetailResponse>();
            var request = new GetExamDetailRequest();
            request.CardNo = cardNo;
            request.ExamId = examId;
            request.ItemType = "D";
            GetExamDetailResponse response;

            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetExamDetail", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<GetExamDetailResponse>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院内获取检查单明细Api出错,信息：{0}", JsonConvert.SerializeObject(ex));
                result.Result = Result.未返回;
                result.Message = "调用院内获取检查单明细Api出错!";
                return result;
            }
            result.Response = response;

            if (response.ResultCode == "0")
            {
                result.Result = Result.成功;
                return result;
            }
            else
            {
                result.Result = Result.失败;
                result.Message = response.ResultDesc;
                return result;
            }

        }

    }
}
